package com.woniu.service;

import com.woniu.adapter.ProductAdapter;
import com.woniu.inlet.fo.ProAddFo;
import com.woniu.inlet.fo.ProductFo;
import com.woniu.outlet.mysql.po.Product;
import com.woniu.service.productcommand.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:理财产品服务类
 * @author: my
 * @time: 2021/4/26 18:51
 */
@Slf4j
@Service
public class ProductService implements GetProductCommandHandler, QueryProductCommandHandler ,
        UpdateProductCommandHandler,AddProductCommandHandler,ProductManageCommandHandler {
    private ProductAdapter productAdapter;

    public ProductService(ProductAdapter productAdapter){
        this.productAdapter = productAdapter;
    }


    @Override
    public Product action(GetProductCommand getProductCommand) {
        Integer id = getProductCommand.getId();
        return productAdapter.getById(id);
    }

    @Override
    public List<Product> action(QueryProductsCommand queryProductsCommand) {
        ProductFo fo = queryProductsCommand.getProductFo();
        return productAdapter.findProducts(fo);
    }

    /**
     * 修改产品信息
     * @param updateProductCommand
     */
    @Override
    public void action(UpdateProductCommand updateProductCommand) {
        Product product = updateProductCommand.getProduct();
        productAdapter.updateProduct(product);
    }

    /**
     * 添加理财产品
     * @param command
     */
    @Override
    public void action(AddProductCommand command) {
        ProAddFo proAddFo = command.getProAddFo();
        Product product = new Product();
        //设置产品参数
        product.setName(proAddFo.getName());
        product.setStartmoney(proAddFo.getStartmoney());
        product.setType(proAddFo.getType());
        product.setDatestr(proAddFo.getDatestr());
        product.setDescription(proAddFo.getDescription());
        product.setExpire(proAddFo.getExpire());
        product.setRisk(proAddFo.getRisk());
        product.setYieldrate(proAddFo.getYieldrate());
        //补充参数
        product.setStatus("n");
        productAdapter.addProduct(product);
        log.debug("产品{}添加成功",product.getName());
    }

    /**
     * 商品上下架
     * @param command
     */
    @Override
    public void action(ProductManageCommand command) {
        Integer id = command.getId();
        Product product = productAdapter.getById(id);
        if ("y".equals(product.getStatus())){
            product.setStatus("d");
        }else {
            product.setStatus("y");
        }
        productAdapter.updateProduct(product);
    }
}
